import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/dashboard',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: '系统首页',
                    permiss: '12',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/dashboard.vue'),
            },
            {
                path: '/supplier',
                name: 'supplier',
                meta: {
                    title: '供应商管理',
                    permiss: '1',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/supplier.vue'),
            },
            {
                path: '/customer',
                name: 'customer',
                meta: {
                    title: '客户管理',
                    permiss: '2',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/customer.vue'),
            },
            {
                path: '/purchase',
                name: 'purchase',
                meta: {
                    title: '采购入库',
                    permiss: '3',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/purchase.vue'),
            },
            {
                path: '/purchaseMes',
                name: 'purchaseMes',
                meta: {
                    title: '采购信息查询',
                    permiss: '4',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/purchaseMes.vue'),
            },
            {
                path: '/sale',
                name: 'sale',
                meta: {
                    title: '销售出库',
                    permiss: '5',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/sale.vue'),
            },
            {
                path: '/saleMes',
                name: 'saleMes',
                meta: {
                    title: '销售信息查询',
                    permiss: '6',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/saleMes.vue'),
            },
            {
                path: '/stock',
                name: 'stock',
                meta: {
                    title: '库存一览',
                    permiss: '7',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/stock.vue'),
            },
            {
                path: '/saleStatistics',
                name: 'saleStatistics',
                meta: {
                    title: '销售统计',
                    permiss: '8',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/saleStatistics.vue'),
            },
            {
                path: '/saleStatisticsMonth',
                name: 'saleStatisticsMonth',
                meta: {
                    title: '月销售统计',
                    permiss: '9',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/saleStatisticsMonth.vue'),
            },
            {
                path: '/user',
                name: 'user',
                meta: {
                    title: '用户管理',
                    permiss: '10',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/user.vue'),
            },
            {
                path: '/role',
                name: 'role',
                meta: {
                    title: '角色管理',
                    permiss: '11',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/role.vue'),
            },
            {
                path: '/password',
                name: 'password',
                meta: {
                    title: '修改密码',
                    permiss: '12',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/password.vue'),
            },
            {
                path: '/journal',
                name: 'journal',
                meta: {
                    title: '日志一览',
                    permiss: '13',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/journal.vue'),
            },
        ],
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/login.vue'),
    },
    {
        path: '/403',
        name: '403',
        meta: {
            title: '没有权限',
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/403.vue'),
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
   document.title = `${to.meta.title} | vue-manage-system`;
    const role = localStorage.getItem('ms_username');
    const permiss = usePermissStore();
    if (!role && to.path !== '/login') {
        next('/login');
    } else {
        next();
    }
});

export default router;
